package com.wx.frame.controller;

import com.alibaba.fastjson.JSON;
import com.wx.frame.model.Menu;
import com.wx.frame.model.Staff;
import com.wx.frame.model.Tree;
import com.wx.frame.model.TreeView;
import com.wx.frame.service.MenuService;
import com.wx.utils.AjaxMessage;
import com.wx.utils.Page;
import com.wx.utils.Pageable;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller("menuController")
@RequestMapping("/menu")
public class MenuController {
	/**
	 * 注入service
	 */
	@Resource(name = "menuServiceImpl")
	public MenuService<Menu> menuService;

    /**
     * 获取用户列表
     * @return
     */
    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
	public Page<Menu> list(Pageable pageable,Menu menu)
	{
    	return menuService.findPage(pageable,menu);
	}



    /**
     * 保存用户操作
     * @parama user 用户实体
     * @return AjaxMessage 保存操作提示消息
     */
    @RequestMapping(value = "/save", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxMessage save(String datas) {
        System.out.println("datas" + datas);
        int result = 0;
        String msg = "";
        try {
            List<Menu> menus = JSON.parseArray("["+datas+"]", Menu.class);
            for (Menu menu : menus) {
                result = this.menuService.save(menu);
            }

            if(1 == result) {
                msg = "保存成功！";
            } else {
                msg = "保存失败！！！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(1 == result ? true : false, msg);
    }

    /**
     * 保存用户操作
     * @parama user 用户实体
     * @return AjaxMessage 保存操作提示消息
     */
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public AjaxMessage update(String datas,String menu_pk) {
        System.out.println("datas" + datas+"menu_pk"+menu_pk);
        int result = 0;
        String msg = "";
        try {
            List<Menu> menus = JSON.parseArray("["+datas+"]", Menu.class);
            for (Menu menu : menus) {
                menu.setMenu_pk(menu_pk);
                result = this.menuService.update(menu);
            }

            if(1 == result) {
                msg = "更新成功！";
            } else {
                msg = "更新失败！！！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AjaxMessage(1 == result ? true : false, msg);
    }
    
    /**
     * 根据主键批量删除
     * @param ids
     */
    @RequestMapping(value = "/delete",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public AjaxMessage delete(String ids)
    {
            int result = 0;
        	String[] tmps = ids.split(",");
        	if(null != tmps && tmps.length > 0) {
        		List<String> idList = new ArrayList<String>();
        		for(String s: tmps) {
        			idList.add(s);
        		}
        		
        		result = this.menuService.delete(idList);
        	}
            String msg = "删除成功！";
            if(1 == result) {
                msg = "删除成功！";
            } else if(2 == result) {
                msg = "删除失败！！！";
            }

            return new AjaxMessage(1 == result ? true : false, msg);
    }


    /**
     * 在菜单添加列表treegrid列表中显示
     * 获取菜单列表
     * @return
     */
    @RequestMapping(value = "/grid_menu", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<Menu> grid_menu()
    {
        List<Menu> menu_roots = new ArrayList<>();//根节点

        Menu menu = new Menu();
        Menu temp_menu= new Menu();//存储临时值
        menu.setMenu_parent("0");
        //获取父菜单
        List<Menu> menus_parents = this.menuService.getMenu(menu);  //查询出所有的父菜单

        for (Menu menu_parent : menus_parents)
        {

            //menu_parent.setMenu_parent(menu_parent.getMenu_pk());//将父菜单的主键作为子菜单的父id

            temp_menu.setMenu_parent(menu_parent.getMenu_pk());
            List<Menu> menus_child = this.menuService.getMenu(temp_menu);//查询出父菜单对应的子菜单


            List<Menu> menu_childs = new ArrayList<>();
            for (Menu menu_child : menus_child)
            {

                /**
                 * 获取页面按钮项
                 */
                //menu_child.setMenu_parent(menu_child.getMenu_pk());
                temp_menu.setMenu_parent(menu_child.getMenu_pk());
                List<Menu> btns_child = this.menuService.getMenu(temp_menu);//查询出父菜单对应的子菜单
                List<Menu> btn_childs = new ArrayList<>();
                 for (Menu menu_btn:btns_child)
                {
                    menu_btn.setLeaf(true);//设置按钮为叶子
                    btn_childs.add(menu_btn);
                }


                menu_child.setChildren(btn_childs);//将按钮放到集合中
                menu_childs.add(menu_child);//取出子菜单并放到集合中
            }
            menu_parent.setChildren(menu_childs);//将子菜单放到childs集合中
            menu_roots.add(menu_parent);
        }
        return menu_roots;
    }


    /**
     * 在权限设置tree列表中显示
     * 获取菜单列表
     * @return
     */
    @RequestMapping(value = "/tree_menu_btns", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<TreeView> tree_menu_btns(@RequestParam("roles_pk")String roles_pk)
    {
        System.out.println("-----------------"+roles_pk);
        List<TreeView> tree_view = tree_menu_roles(roles_pk);
        List<TreeView> tree_init = tree_menu_init();
        if (tree_view != null && tree_view.size()>0)
        {
            return tree_view;
        }
        else
        {
            return tree_init;
        }
    }

    public List<TreeView> tree_menu_roles(@Param("roles_pk")String roles_pk)
    {
        List<Menu> menu_roots = new ArrayList<>();//根节点
        List<TreeView> tree_roots = new ArrayList<>();

        Menu menu = new Menu();
        menu.setMenu_parent("0");
        List<Menu> menus_parents = this.menuService.getCheckedMenu(menu,roles_pk);  //查询出所有的父菜单

        for (Menu menu_parent : menus_parents)
        {

            TreeView tree_parent = new TreeView();
            tree_parent.setId(menu_parent.getMenu_pk());
            tree_parent.setText(menu_parent.getMenu_name());
            tree_parent.setIcon(menu_parent.getMenu_icon());
            tree_parent.setUrl(menu_parent.getMenu_url());
            tree_parent.setChecked(menu_parent.isChecked());
            menu_parent.setMenu_parent(menu_parent.getMenu_pk());//将父菜单的主键作为子菜单的父id
            List<Menu> menus_child = this.menuService.getCheckedMenu(menu_parent,roles_pk);//查询出父菜单对应的子菜单


            List<Menu> menu_childs = new ArrayList<>();
            List<TreeView> tree_childs = new ArrayList<>();

            for (Menu menu_child : menus_child)
            {

                TreeView tree_child = new TreeView();
                tree_child.setId(menu_child.getMenu_pk());
                tree_child.setText(menu_child.getMenu_name());
                tree_child.setIcon(menu_child.getMenu_icon());
//                tree_child.setLeaf(false);
                tree_child.setUrl(menu_child.getMenu_url());
                tree_child.setChecked(menu_child.isChecked());
                tree_childs.add(tree_child);//取出子菜单并放到集合中

                /**
                 * 获取页面按钮项
                 */
                menu_child.setMenu_parent(menu_child.getMenu_pk());
                List<Menu> btns_child = this.menuService.getCheckedMenu(menu_child,roles_pk);//查询出父菜单对应的子菜单
                List<Menu> btn_childs = new ArrayList<>();
                List<TreeView> btn_tree_childs = new ArrayList<>();

                for (Menu menu_btn:btns_child)
                {
                    TreeView btn_child = new TreeView();
                    btn_child.setId(menu_btn.getMenu_pk());
                    btn_child.setText(menu_btn.getMenu_name());
                    btn_child.setIcon(menu_btn.getMenu_icon());
                    btn_child.setLeaf(true);  //设置按钮为叶子节点
                    btn_child.setUrl(menu_btn.getMenu_url());
                    btn_child.setChecked(menu_btn.isChecked());
                    btn_tree_childs.add(btn_child);//取出按钮项并放到集合中

//                    menu_btn.setLeaf(true);//设置按钮为叶子
//                    btn_childs.add(menu_btn);
                }

                tree_child.setChildren(btn_tree_childs);
//                tree_childs.add(tree_child);
//                menu_child.setChildren(btn_childs);//将按钮放到集合中
//                menu_childs.add(menu_child);//取出子菜单并放到集合中

            }
//            menu_parent.setChildren(menu_childs);//将子菜单放到childs集合中
//            menu_roots.add(menu_parent);
            tree_parent.setChildren(tree_childs);//将子菜单放到childs集合中
            tree_roots.add(tree_parent);
        }
        return tree_roots;
    }

    public List<TreeView> tree_menu_init()
    {
        List<Menu> menu_roots = new ArrayList<>();//根节点
        List<TreeView> tree_roots = new ArrayList<>();

        Menu menu = new Menu();
        menu.setMenu_parent("0");
        List<Menu> menus_parents = this.menuService.getMenu(menu);  //查询出所有的父菜单

        for (Menu menu_parent : menus_parents)
        {

            TreeView tree_parent = new TreeView();
            tree_parent.setId(menu_parent.getMenu_pk());
            tree_parent.setText(menu_parent.getMenu_name());
            tree_parent.setIcon(menu_parent.getMenu_icon());
            tree_parent.setUrl(menu_parent.getMenu_url());

            menu_parent.setMenu_parent(menu_parent.getMenu_pk());//将父菜单的主键作为子菜单的父id
            List<Menu> menus_childs = this.menuService.getMenu(menu_parent);//查询出父菜单对应的子菜单


            List<Menu> menu_childs = new ArrayList<>();
            List<TreeView> tree_childs = new ArrayList<>();

            for (Menu menu_child : menus_childs)
            {

                TreeView tree_child = new TreeView();
                tree_child.setId(menu_child.getMenu_pk());
                tree_child.setText(menu_child.getMenu_name());
                tree_child.setIcon(menu_child.getMenu_icon());
//                tree_child.setLeaf(false);
                tree_child.setUrl(menu_child.getMenu_url());
                tree_childs.add(tree_child);//取出子菜单并放到集合中

                /**
                 * 获取页面按钮项
                 */
                menu_child.setMenu_parent(menu_child.getMenu_pk());
                List<Menu> btns_child = this.menuService.getMenu(menu_child);//查询出父菜单对应的子菜单
                List<Menu> btn_childs = new ArrayList<>();
                List<TreeView> btn_tree_childs = new ArrayList<>();

                for (Menu menu_btn:btns_child)
                {
                    TreeView btn_child = new TreeView();
                    btn_child.setId(menu_btn.getMenu_pk());
                    btn_child.setText(menu_btn.getMenu_name());
                    btn_child.setIcon(menu_btn.getMenu_icon());
                    btn_child.setLeaf(true);  //设置按钮为叶子节点
                    btn_child.setUrl(menu_btn.getMenu_url());
                    btn_tree_childs.add(btn_child);//取出按钮项并放到集合中

//                    menu_btn.setLeaf(true);//设置按钮为叶子
//                    btn_childs.add(menu_btn);
                }

                tree_child.setChildren(btn_tree_childs);
//                tree_childs.add(tree_child);
//                menu_child.setChildren(btn_childs);//将按钮放到集合中
//                menu_childs.add(menu_child);//取出子菜单并放到集合中

            }
//            menu_parent.setChildren(menu_childs);//将子菜单放到childs集合中
//            menu_roots.add(menu_parent);
            tree_parent.setChildren(tree_childs);//将子菜单放到childs集合中
            tree_roots.add(tree_parent);
        }
        return tree_roots;
    }

    /**
     *导航菜单中显示菜单列表
     * 获取菜单列表
     * @return
     */
    @RequestMapping(value = "/list_menu", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<Tree> list_menu(HttpSession session) {
        //获取session
        Staff staff = (Staff) session.getAttribute("loginUser");
        String roles_pk = staff.getFk_roles_pk();

        //定义根节点
        List<Tree> tree_roots = new ArrayList<>();

        Menu menu = new Menu();
        menu.setMenu_parent("0");
        List<Menu> menus_parents = this.menuService.getRolesMenu(menu, roles_pk);  //查询出所有的父菜单
        if (null != menus_parents && menus_parents.size() > 0)
        {
            for (Menu menu_parent : menus_parents) {
                Tree tree_parent = new Tree();
                tree_parent.setId(menu_parent.getMenu_pk());
                tree_parent.setIcon(menu_parent.getMenu_icon());
                tree_parent.setText(menu_parent.getMenu_name());

                menu_parent.setMenu_parent(menu_parent.getMenu_pk());//将父菜单的主键作为子菜单的父id
                List<Menu> menus_child = this.menuService.getRolesMenu(menu_parent, roles_pk);//查询出父菜单对应的子菜单

                if (null != menus_child && menus_child.size()>0) {
                    List<Tree> tree_childs = new ArrayList<>();
                    for (Menu menu_child : menus_child) {
                        Tree tree_child = new Tree();
                        tree_child.setId(menu_child.getMenu_pk());
                        tree_child.setText(menu_child.getMenu_name());
                        tree_child.setIcon(menu_child.getMenu_icon());
                        tree_child.setLeaf(true);
                        tree_child.setUrl(menu_child.getMenu_url());
                        tree_childs.add(tree_child);//取出子菜单并放到集合中
                    }
                    tree_parent.setChildren(tree_childs);//将子菜单放到childs集合中
                }
                tree_roots.add(tree_parent);
            }

        }
        return tree_roots;
    }

}

